package com.bauer.springboot.starter.inerface.impl;

import com.alibaba.fastjson.JSON;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.bauer.springboot.starter.inerface.AliMQProducer;
import com.bauer.springboot.starter.props.AliMQProperties;

/**
 * @description:
 * @author: weirui
 * @create: 2020-01-17 14:03
 **/
public class DefaultAliProducerImpl implements AliMQProducer {


    private AliMQProperties aliMQProperties;

    private ProducerBean producerBean;

    public DefaultAliProducerImpl(AliMQProperties aliMQProperties,
        ProducerBean producerBean) {
        this.aliMQProperties = aliMQProperties;
        this.producerBean = producerBean;
    }

    @Override
    public void sendMessage(Object msg, String topic, String tag) {
        producerBean.send(new Message(topic, tag, JSON.toJSONBytes(msg)));
    }

    @Override
    public void sendMessage(Object msg, String tag) {
        sendMessage(msg, aliMQProperties.getTopic(), tag);
    }
}
